<?php


echo '<h2 class="accessibility">'.$this->Lang('install_gateway').'</h2>';


if(Antz_IntelliForm::submitted('install-gateway')){
	$error = false;
	$className = post('className');
	$safeClassName = preg_replace('/[^a-zA-Z0-9_]/', '', $className);
	$title = post('title');
	$desc = post('desc');
	$isActive = (int) post('isActive');
	$file = post('file');

	if(trim($title)==''){
		$error = true;
		Antz::addFormError('title');
	};

	if(trim($desc)==''){
		$error = true;
		Antz::addError($this->Lang('no_description'));
		Antz::addFormError('desc');
	};

	if($safeClassName != $className){
		$error = true;
		Antz::addFormError('className', $this->Lang('invalid_chars'));
	};

	if(class_exists($className)){
		$error = true;
		Antz::addFormError('className', $this->Lang('class_exists'));
	};

	if($DB->countRows(cms_db_prefix().'isubscribe_payment_gateways', "class_name = '{$safeClassName}'")>0){
		$error = true;
		Antz::addFormError('className', $this->Lang('already_registered_class'));
	};

	$filename = $this->dirname.'/gateways/'.$className.'.php';

	if(!file_exists($file['tmp_name'])){
		$error = true;
		Antz::addFormError('file', $this->Lang('not_attached'));
	};
	if(file_exists($filename)){
		$error = true;
		Antz::addFormError('file', $this->Lang('file_exists', $filename));
	}else{
		require_once($file['tmp_name']);
		if(!class_exists($safeClassName)){
			$error = true;
			Antz::addFormError('file', $this->Lang('class_not_found', $safeClassName));
		}else{
			$obj = new $safeClassName;
			if(!($obj instanceof ISubscribe_Payment_Gateway)){
				$error = true;
				Antz::addFormError('file', $this->Lang('class_must_extend', $safeClassName));
			};
		};
	};

	if(!$error){
		copy($file['tmp_name'], $filename);
		if(!class_exists($className)){
			require_once($filename);
			$obj = new $className;
		};

		$dbParams = array(
			'title' => $title,
			'class_name' => $className,
			'obj' => $CRYPT->crypt(serialize($obj)),
			'description' => $desc,
			'is_active' => $isActive
		);

		$gwId = $DB->insert(cms_db_prefix().'isubscribe_payment_gateways', $dbParams);

		Antz::addSuccess($this->Lang('gateway_installed'));
		Antz::redir($Antz->addParamsToUrl($actionUrls['config_gateway'], array('gw_id'=>$gwId)));

	}

}

$form = Antz::newObject('Antz_Form', 'install-gateway');
$form->setAction($actionUrls['install_gateway']);

$block = $form->add('full');
$fs = $block->add($this->Lang('gateway_legend'));

$fs->addInput('text', 'className', $this->lang('classname'), post('className'));
$fs->addInput('file', 'file', 'Class file', post('file'));
$chk = $fs->addCheckboxGroup('isActive');
$chk->add('isActive', $this->Lang('is_active'), '1', post('isActive', '0'));

$fs->addInput('text', 'title', $this->Lang('title'), post('title'));

$txt = $fs->addInput('textarea', 'desc', $this->Lang('desc'), post('desc'));
$txt->setId('install-gateway');
$mces = explode(',', $_SESSION['tiny_live_textareas']);
if(!in_array('install-gateway', $mces)) $mces[] = 'install-gateway';
$_SESSION['tiny_live_textareas'] = implode(',', $mces);

$fs->addInput('submit', 'submitBtn', '&nbsp;', $this->Lang('install_button'));

$this->smarty->assign('form', $form->render());

echo $this->ProcessTemplate('admin.install-gateway.php');
